package com.gaogzhen.security.syn;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j(topic = "security.syn.TestTransfer")
public class TestTransfer {
    public static void main(String[] args) throws InterruptedException {
        Account a = new Account(1000);
        Account b = new Account(1000);
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 500; i++) {
                a.transfer(b, randomInt());
            }
        }, "t1");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 500; i++) {
                a.transfer(b, randomInt());
            }
        }, "t2");

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        log.debug("总金额：{}", (a.getMoney() + b.getMoney()));
    }



    static Random r = new Random();
    public static int randomInt() { return r.nextInt(100) + 1;}
}
